class_name CDataDefineMgr

# if g_Globals.IsClient :
    # require "TableData.COtherDataDefine"
    # require "TableData.CPlayerDataDefine"
    # require "TableData.CPlayermailDataDefine"
    # require "TableData.CGlobalmailDataDefine"
    # require "TableData.CPubDataDefine"
    # require "TableData.CRankDataDefine"


# require "TableData.TableCommonDefine"

static var g_tbl_ID2DateDefine={
    TableCommonDefine.EDataDefineType.eDDT_Other : COtherDataDefine,
    TableCommonDefine.EDataDefineType.eDDT_Player : CPlayerDataDefine,
    TableCommonDefine.EDataDefineType.eDDT_Playermail : CPlayermailDataDefine,
    TableCommonDefine.EDataDefineType.eDDT_Globalmail : CGlobalmailDataDefine,
    TableCommonDefine.EDataDefineType.eDDT_Pub : CPubDataDefine,
    TableCommonDefine.EDataDefineType.eDDT_Rank : CRankDataDefine,
}

static var  g_tbl_ID2DateDefineName={
    TableCommonDefine.EDataDefineType.eDDT_Other : "COtherDataDefine",
    TableCommonDefine.EDataDefineType.eDDT_Player : "CPlayerDataDefine",
    TableCommonDefine.EDataDefineType.eDDT_Playermail : "CPlayermailDataDefine",
    TableCommonDefine.EDataDefineType.eDDT_Globalmail : "CGlobalmailDataDefine",
    TableCommonDefine.EDataDefineType.eDDT_Pub : "CPubDataDefine",
    TableCommonDefine.EDataDefineType.eDDT_Rank : "CRankDataDefine",
}
var m_AllOtherDataDefine = {}
var m_AllPlayerDataDefine = {}
var m_AllPlayermailDataDefine = {}
var m_AllGlobalmailDataDefine = {}
var m_AllPubDataDefine = {}
var m_AllRankDataDefine = {}


var m_AllDataDefine

func _init():
    self.m_AllDataDefine = {
        TableCommonDefine.EDataDefineType.eDDT_Other : self.m_AllOtherDataDefine,
        TableCommonDefine.EDataDefineType.eDDT_Player : self.m_AllPlayerDataDefine,
        TableCommonDefine.EDataDefineType.eDDT_Playermail : self.m_AllPlayermailDataDefine,
        TableCommonDefine.EDataDefineType.eDDT_Globalmail : self.m_AllGlobalmailDataDefine,
        TableCommonDefine.EDataDefineType.eDDT_Pub : self.m_AllPubDataDefine,
        TableCommonDefine.EDataDefineType.eDDT_Rank : self.m_AllRankDataDefine,

    }


func GetData(_nDataDefineType, _uuid):
    var typeData = self.m_AllDataDefine[_nDataDefineType]
    if not typeData :
        return
    
    return typeData[_uuid]


func AllocDataDefinByType( _nDataDefineType, _uuid, _context):
    var dataClass = g_tbl_ID2DateDefine[_nDataDefineType]
    var typeData = self.m_AllDataDefine[_nDataDefineType]
    if not dataClass or not typeData :
        print( "AllocDataDefinByType error", _nDataDefineType )
        return
    
    var newData = dataClass.new(_uuid, _context)
    typeData[_uuid] = newData
    return newData



func FreeDataDefinByType(_nDataDefineType, _uuid):
    var allTypeDataDefine = self.m_AllDataDefine[_nDataDefineType]
    if not allTypeDataDefine :
        print( "[CDataDefineMgr] error:DestroyInitData", _nDataDefineType, _uuid )
        return false
    
    var data = allTypeDataDefine[_uuid]
    if data :
        allTypeDataDefine[_uuid] = null
        data.ClearData()
    
    return true


func DestroyInitData(_tblMsg, _pCurrent):
    var nDataDefineType = _tblMsg.dataDefineType
    var uuid = _tblMsg.dataDefineUUID
    self.FreeDataDefinByType( nDataDefineType, uuid )



func AllocOtherDataDefine(_uuid, _context):
    if self.m_AllOtherDataDefine.has(_uuid) :
        print( "[CDataDefineMgr] warning:allocOther", _uuid )
        return self.m_AllOtherDataDefine[_uuid]
    
    var data = COtherDataDefine.new(_uuid, null)
    self.m_AllOtherDataDefine[_uuid] = data
    return data


func FreeOtherDataDefine(_uuid):
    if not self.m_AllOtherDataDefine.has(_uuid) :
        print( "[CDataDefineMgr] error:freeOther", _uuid )
        return false
    
    var data = self.m_AllOtherDataDefine[_uuid]
    self.m_AllOtherDataDefine[_uuid] = null
    data.ClearData()
    return true


func GetOtherDataDefine(_uuid):
    return self.m_AllOtherDataDefine[_uuid]


func AllocPlayerDataDefine(_uuid, _context):
    if self.m_AllPlayerDataDefine.has(_uuid) :
        print( "[CDataDefineMgr] warning:allocPlayer", _uuid )
        return self.m_AllOtherDataDefine[_uuid]
    
    var data = CPlayerDataDefine.new(_uuid, null)
    self.m_AllPlayerDataDefine[_uuid] = data
    return data


func FreePlayerDataDefine(_uuid):
    if not self.m_AllPlayerDataDefine.has(_uuid) :
        print( "[CDataDefineMgr] error:freePlayer", _uuid )
        return false
    
    var data = self.m_AllPlayerDataDefine[_uuid]
    self.m_AllPlayerDataDefine[_uuid] = null
    data.ClearData()
    return true


func GetPlayerDataDefine(_uuid):
    return self.m_AllPlayerDataDefine[_uuid]


func AllocPlayermailDataDefine(_uuid, _context):
    if self.m_AllPlayermailDataDefine.has(_uuid) :
        print( "[CDataDefineMgr] warning:allocPlayermail", _uuid )
        return self.m_AllOtherDataDefine[_uuid]
    
    var data = CPlayermailDataDefine.new(_uuid, null)
    self.m_AllPlayermailDataDefine[_uuid] = data
    return data


func FreePlayermailDataDefine(_uuid):
    if not self.m_AllPlayermailDataDefine.has(_uuid) :
        print( "[CDataDefineMgr] error:freePlayermail", _uuid )
        return false
    
    var data = self.m_AllPlayermailDataDefine[_uuid]
    self.m_AllPlayermailDataDefine[_uuid] = null
    data.ClearData()
    return true


func GetPlayermailDataDefine(_uuid):
    return self.m_AllPlayermailDataDefine[_uuid]


func AllocGlobalmailDataDefine(_uuid, _context):
    if self.m_AllGlobalmailDataDefine.has(_uuid) :
        print( "[CDataDefineMgr] warning:allocGlobalmail", _uuid )
        return self.m_AllOtherDataDefine[_uuid]
    
    var data = CGlobalmailDataDefine.new(_uuid, null)
    self.m_AllGlobalmailDataDefine[_uuid] = data
    return data


func FreeGlobalmailDataDefine(_uuid):
    if not self.m_AllGlobalmailDataDefine.has(_uuid) :
        print( "[CDataDefineMgr] error:freeGlobalmail", _uuid )
        return false
    
    var data = self.m_AllGlobalmailDataDefine[_uuid]
    self.m_AllGlobalmailDataDefine[_uuid] = null
    data.ClearData()
    return true


func GetGlobalmailDataDefine(_uuid):
    return self.m_AllGlobalmailDataDefine[_uuid]


func AllocPubDataDefine(_uuid, _context):
    if self.m_AllPubDataDefine.has(_uuid) :
        print( "[CDataDefineMgr] warning:allocPub", _uuid )
        return self.m_AllOtherDataDefine[_uuid]
    
    var data = CPubDataDefine.new(_uuid, null)
    self.m_AllPubDataDefine[_uuid] = data
    return data


func FreePubDataDefine(_uuid):
    if not self.m_AllPubDataDefine.has(_uuid) :
        print( "[CDataDefineMgr] error:freePub", _uuid )
        return false
    
    var data = self.m_AllPubDataDefine[_uuid]
    self.m_AllPubDataDefine[_uuid] = null
    data.ClearData()
    return true


func GetPubDataDefine(_uuid):
    return self.m_AllPubDataDefine[_uuid]


func AllocRankDataDefine(_uuid, _context):
    if self.m_AllRankDataDefine.has(_uuid) :
        print( "[CDataDefineMgr] warning:allocRank", _uuid )
        return self.m_AllOtherDataDefine[_uuid]
    
    var data = CRankDataDefine.new(_uuid, null)
    self.m_AllRankDataDefine[_uuid] = data
    return data


func FreeRankDataDefine(_uuid):
    if not self.m_AllRankDataDefine.has(_uuid) :
        print( "[CDataDefineMgr] error:freeRank", _uuid )
        return false
    
    var data = self.m_AllRankDataDefine[_uuid]
    self.m_AllRankDataDefine[_uuid] = null
    data.ClearData()
    return true


func GetRankDataDefine(_uuid):
    return self.m_AllRankDataDefine[_uuid]



